package com.edu.bhu.admin.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.edu.bhu.admin.domain.BikImages;
import com.edu.bhu.admin.mapper.BikImagesMapper;
import com.edu.bhu.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.edu.bhu.admin.mapper.BikBicycleMapper;
import com.edu.bhu.admin.domain.BikBicycle;
import com.edu.bhu.admin.service.IBikBicycleService;
import com.edu.bhu.common.core.text.Convert;

/**
 * 单车Service业务层处理
 * 
 * @author heyinan
 * @date 2022-03-14
 */
@Service
public class BikBicycleServiceImpl implements IBikBicycleService 
{
    @Autowired
    private BikBicycleMapper bikBicycleMapper;

    @Autowired
    private BikImagesMapper bikImagesMapper;

    /**
     * 查询单车
     * 
     * @param bikId 单车主键
     * @return 单车
     */
    @Override
    public BikBicycle selectBikBicycleByBikId(Long bikId)
    {
        return bikBicycleMapper.selectBikBicycleByBikId(bikId);
    }

    /**
     * 查询单车列表
     * 
     * @param bikBicycle 单车
     * @return 单车
     */
    @Override
    public List<BikBicycle> selectBikBicycleList(BikBicycle bikBicycle)
    {
        return bikBicycleMapper.selectBikBicycleList(bikBicycle);
    }

    /**
     * 查询单车列表伴随第一张图片，不显示已经被出租的和停用的
     *
     * @param bikBicycle 单车
     * @return 单车集合
     */
    @Override
    public List<BikBicycle> selectBikBicycleListWithImg(BikBicycle bikBicycle)
    {
        List<BikBicycle> list = bikBicycleMapper.selectBikBicycleList(bikBicycle);
        list.stream().map(item -> {
            List<BikImages> images = bikImagesMapper.selectBikImagesListByBikId(item.getBikId());
            //获取图片列表中的第一张
            if(images.size()!=0)
                item.setBikImages(images.get(0));
            return item;
        }).collect(Collectors.toList());
        return list;
    }

    /**
     * 新增单车
     * 
     * @param bikBicycle 单车
     * @return 结果
     */
    @Override
    public int insertBikBicycle(BikBicycle bikBicycle)
    {
        bikBicycle.setUpdateDate(DateUtils.getNowDate());
        return bikBicycleMapper.insertBikBicycle(bikBicycle);
    }

    /**
     * 修改单车
     * 
     * @param bikBicycle 单车
     * @return 结果
     */
    @Override
    public int updateBikBicycle(BikBicycle bikBicycle)
    {
        bikBicycle.setUpdateDate(DateUtils.getNowDate());
        return bikBicycleMapper.updateBikBicycle(bikBicycle);
    }

    /**
     * 单车状态修改
     *
     * @param bikBicycle 单车信息
     * @return 结果
     */
    public int changeStatus(BikBicycle bikBicycle)
    {
        bikBicycle.setUpdateDate(DateUtils.getNowDate());
        return bikBicycleMapper.updateBikBicycle(bikBicycle);
    }

    /**
     * 批量删除单车
     * 
     * @param bikIds 需要删除的单车主键
     * @return 结果
     */
    @Override
    public int deleteBikBicycleByBikIds(String bikIds)
    {
        return bikBicycleMapper.deleteBikBicycleByBikIds(Convert.toStrArray(bikIds));
    }

    /**
     * 删除单车信息
     * 
     * @param bikId 单车主键
     * @return 结果
     */
    @Override
    public int deleteBikBicycleByBikId(Long bikId)
    {
        return bikBicycleMapper.deleteBikBicycleByBikId(bikId);
    }
}
